package com.yxjwzpx.management.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.yxjwzpx.management.domain.MaintenanceStaff;
import com.yxjwzpx.management.mapper.MaintainCompanyMapper;
import com.yxjwzpx.management.mapper.MaintenanceStaffMapper;
import com.yxjwzpx.management.service.IMaintenanceStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 维保人员Service业务层处理
 * 
 * @author yxjwzpx
 * @date 2025-04-09
 */
@Service
public class MaintenanceStaffServiceImpl implements IMaintenanceStaffService 
{
    @Autowired
    private MaintenanceStaffMapper maintenanceStaffMapper;
    @Autowired
    private MaintainCompanyMapper maintainCompanyMapper;
    /**
     * 查询维保人员
     * 
     * @param staffId 维保人员主键
     * @return 维保人员
     */
    @Override
    public MaintenanceStaff selectMaintenanceStaffByStaffId(Long staffId)
    {
        return maintenanceStaffMapper.selectMaintenanceStaffByStaffId(staffId);
    }

    /**
     * 查询维保人员列表
     * 
     * @param maintenanceStaff 维保人员
     * @return 维保人员
     */
    @Override
    public List<MaintenanceStaff> selectMaintenanceStaffList(MaintenanceStaff maintenanceStaff)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        List<SysRole> roles = user.getRoles();

        if("COMPANY".equals(roles.get(0).getRoleKey())){
            Long companyId = maintainCompanyMapper.selectCompanyIdByUserId(user.getUserId());
            if(companyId!=null){
                maintenanceStaff.setCompanyId(companyId);
            }else {
                return new ArrayList<>();
            }
        }
        return maintenanceStaffMapper.selectMaintenanceStaffList(maintenanceStaff);
    }

    /**
     * 新增维保人员
     * 
     * @param maintenanceStaff 维保人员
     * @return 结果
     */
    @Override
    public int insertMaintenanceStaff(MaintenanceStaff maintenanceStaff)
    {
        maintenanceStaff.setCreateTime(DateUtils.getNowDate());
        return maintenanceStaffMapper.insertMaintenanceStaff(maintenanceStaff);
    }

    /**
     * 修改维保人员
     * 
     * @param maintenanceStaff 维保人员
     * @return 结果
     */
    @Override
    public int updateMaintenanceStaff(MaintenanceStaff maintenanceStaff)
    {
        maintenanceStaff.setUpdateTime(DateUtils.getNowDate());
        return maintenanceStaffMapper.updateMaintenanceStaff(maintenanceStaff);
    }

    /**
     * 批量删除维保人员
     * 
     * @param staffIds 需要删除的维保人员主键
     * @return 结果
     */
    @Override
    public int deleteMaintenanceStaffByStaffIds(Long[] staffIds)
    {
        return maintenanceStaffMapper.deleteMaintenanceStaffByStaffIds(staffIds);
    }

    /**
     * 删除维保人员信息
     * 
     * @param staffId 维保人员主键
     * @return 结果
     */
    @Override
    public int deleteMaintenanceStaffByStaffId(Long staffId)
    {
        return maintenanceStaffMapper.deleteMaintenanceStaffByStaffId(staffId);
    }
}
